package com.yonyou.gmmc.service.points.eventhandle;

import org.marketing.common.dto.EventDataDTO;
import org.marketing.common.event.BizEvent;
import org.marketing.common.event.EventRecommandCarOrderedDTO;
import org.marketing.common.event.enums.EventConstant;
import org.marketing.common.event.enums.EventConstant.EventBizStep;
import org.marketing.common.event.enums.EventConstant.EventBizType;
import org.marketing.common.util.MapConverUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yonyou.gmmc.service.points.exception.PointsException;
import com.yonyou.gmmc.service.points.model.TmCarRecommendPoints;
import com.yonyou.gmmc.service.points.service.PointsPayService;
import com.yonyou.gmmc.service.points.service.PointsRuleService;
import com.yonyou.gmmc.service.points.util.SpringUtil;

/**
 * 推荐购车下单事件
 * 
 * @author BENJAMIN
 *
 */
public class RecommandCarOrderedEventHandler extends AbstractEventHandle {

	private static Logger logger = LoggerFactory.getLogger(RecommandCarOrderedEventHandler.class);

	// 推荐购车
	private static EventBizType BizType = EventConstant.EventBizType.RECOMMAND_CARORDER;
	// 被推荐购车人下单
	private static EventBizStep BizStep = EventConstant.EventBizStep.RECOMMAND_CARORDER_ORDERED;

	// @Autowired
	// private PointsRuleService pointsRuleService;

	@Override
	protected String getHandleInfo() {
		return "推荐购车下单成功";
	}

	
	/**
	 * 主要进行数据转换
	 */
	@Override
	protected EventRecommandCarOrderedDTO preTransform(BizEvent event) {
		EventRecommandCarOrderedDTO o = new EventRecommandCarOrderedDTO();
		try {
			o = (EventRecommandCarOrderedDTO) MapConverUtil.map2POStrict(event.getEventData(), o);
			logger.info(o.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			this.backInfo = "eventData中的业务数据不能识别：" + e.getMessage();
			throw new PointsException(PointsException.EVENT_CONVERT_FAILD,"eventData中的业务数据不能识别");
		}
		return o;
	}

	/**
	 * 积分处理的逻辑
	 */
	@Override
	protected boolean pay(EventDataDTO eventData) throws PointsException{
		
		//获取到业务数据
		EventRecommandCarOrderedDTO thisEventData = (EventRecommandCarOrderedDTO)eventData;
		
		PointsRuleService pointsRuleService=(PointsRuleService)SpringUtil.getBean("pointsRuleService");
		
		//获取到规则
		//购车比较特殊 ，根据数量来获取积分
		TmCarRecommendPoints pointsRule = pointsRuleService.getPointsByNum(thisEventData.getRecommandNum());
		
		if(pointsRule!=null){
			Long points = pointsRule.getPointsNum();
			PointsPayService pointsPayService=(PointsPayService)SpringUtil.getBean("pointsPayService");
			
			pointsPayService.payPointsToUser(points, 
					thisEventData.getFromUserId(),
					thisEventData.getPhone(),
					this.getHandleInfo(),
					String.valueOf(thisEventData.getRecommandBizNo()),
					BizStep.getCode(),
					thisEventData.getPhone(),
					String.valueOf(thisEventData.getToUserId()),
					String.valueOf(thisEventData.getRecommandNum())
					);
		}else{
			this.backInfo = "未找到对应的积分规则：";
			throw new PointsException(PointsException.RULE_NOTFOUND,"未找到对应的积分规则");
		}
		return false;
	}


}
